package cn.wikiflyer.beikang.act.user;

import java.lang.reflect.Type;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.IApplication;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.R.color;
import cn.wikiflyer.beikang.bean.NoticeBean;
import cn.wikiflyer.beikang.bean.NoticeListBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.utils.L;
import cn.wk.libs4a.utils.WKTimeUtils;
import cn.wk.libs4a.view.list.WKListViewInterface;
import cn.wk.libs4a.view.list.WKListViewUtils;
import cn.wk.libs4a.view.list.WKViewHolder;
import cn.wk.libs4a.view.pulltorefresh.PullToRefreshListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 我的通知
 * 
 * @author qunlee_mr
 * 
 */
public class UserNoticeListAct extends BaseActivity implements
		WKListViewInterface, OnItemClickListener {

	private WKListViewUtils<NoticeBean, NoticeListBean> listUtils;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_order_list_layout);// common_layout布局作为列表通用布局
	}

	@Override
	public void init() {
		getHeader().setTitle("我的通知");
		getHeader().setLeftAsBack();
		getHeader().setLeftImg("全部已读", R.drawable.ico_read_all, false, 12);
		getHeader().setClickListener(null, null, new OnClickListener() {
			public void onClick(View arg0) {
//				WKApplication.showMsg("全部已读");
				if (listUtils.bean != null && listUtils.bean.data != null &&listUtils.bean.data.list != null&& listUtils.bean.data.list.size() > 0) {
					for (NoticeBean mNoticeBean :listUtils.bean.data.list) {
						if (mNoticeBean.status.equals("1")) {
							new GetReadNoticeTask(ctx, mNoticeBean.messageid);
						}
					}
					listUtils.updateData();
				}else{
					Toast.makeText(ctx, "没有通知", Toast.LENGTH_SHORT).show();
				}
			}
		});
		initList();
	}

	@Override
	public void setData() {
		listUtils.updateData();
	}

	private void initList() {
		PullToRefreshListView pList = (PullToRefreshListView) findViewById(R.id.wk_pull_refresh_list);
		listUtils = new WKListViewUtils<NoticeBean, NoticeListBean>() {
		};
		listUtils.init(ctx, this, pList);
		listUtils.pullRefreshListView.setOnItemClickListener(this);
	}

	@Override
	public String getData(boolean... isLocal) {
//		app().net.setLoginCookie(true);
		return app().net.getNoticeList("", listUtils.page, listUtils.pageCount,
				isLocal);
	}

	@Override
	public void getDataBack(String result, boolean isLocal) {
		Type cvbType = new TypeToken<BKBaseBean<NoticeListBean>>() {
		}.getType();
		Gson gson = new Gson();
		listUtils.bean = gson.fromJson(result, cvbType);
		// 使用数据
		listUtils.list.addAll(listUtils.bean.data.list);
		listUtils.adapter.notifyDataSetChanged();
	}

	@Override
	public WKViewHolder createViewHolder(View arg1) {
		return new MyViewHolder(arg1);
	}

	@Override
	public int getItemViewId() {
		return R.layout.act_notice_list_item;
	}

	class MyViewHolder extends WKViewHolder<NoticeBean> {
		TextView notice_type;
		TextView notice_time;
		TextView notice_detail;

		public MyViewHolder(View view) {
			notice_type = (TextView) view.findViewById(R.id.noticeitem_title);
			notice_time = (TextView) view.findViewById(R.id.noticeitem_time);
			notice_detail = (TextView) view
					.findViewById(R.id.noticeitem_detail);
		}

		@Override
		public void setData(NoticeBean t) {
			L.wj("status :"+t.status);
			if (t.status.equals("1")) {
				notice_type.setTextColor(getResources().getColor(R.color.black));
				notice_detail.setTextColor(getResources().getColor(R.color.black));
			}else{
				notice_type.setTextColor(getResources().getColor(R.color.color_read));
				notice_detail.setTextColor(getResources().getColor(R.color.color_read));
			}
			if (t.send_from_id.equals("admin")) {
				notice_type.setText("系统 - " + t.subject);
			} else {
				notice_type.setText("用户 - " + t.subject);
			}
			notice_time.setText(WKTimeUtils.getTimeFromLongEndWithMM(Long
					.valueOf(t.message_time) * 1000));
			notice_detail.setText(t.content);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(ctx, UserNoticeInfoAct.class);
		intent.putExtra("NoticeBean", listUtils.list.get(arg2 - 1));
		startActivity(intent);
	}
	/**读取通知线程
	 */
	class GetReadNoticeTask extends WKAsyncTaskPro {
		private String messageId;
		public GetReadNoticeTask(Context context,String messageId) {
			super(context);
			this.messageId = messageId;
		}
		@Override
		protected String doInBackground(boolean... params) {
			return app().net.readNotice(messageId);
		}
		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean<NoticeBean>>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean<NoticeBean> b = gson.fromJson(result, cvbType);
//			if (b.status == 1) {
//			}
		}
	}

}
